<?php
/**
 * Created by PhpStorm.
 * User: jieai
 * Date: 2017-03-28
 * Time: 22:17
 */
namespace Admin\Controller;

use Think\Controller;
use Org\Util\Rbac;

class RbacController extends Controller


{


    public function _initialize()
    {
        if (!isset($_SESSION[C('USER_AUTH_KEY')])) {
            $this->error('你没有权限', U('Admin/Login/Login'));
        }
        if (C('USER_AUTH_ON')) {
            import('Org.Util.rbac');

            dump(Rbac::AccessDecision());
        }
    }

    public function User()
    {
            $result= D('UserRelation')->relation(true)->select();


        $this->assign('list',$result);
        $this->display();
    }

    public function Role()
    {
        $list = M('role')->select();
        $this->assign('list', $list);
        $this->display();
    }

    public function Node()

    {
        $field = array('id', 'name', 'title', 'pid', 'level');
        $node = M('node')->field($field)->select();
//       p(sortnode($node));
//       die;
        $arr = sortnode($node);
        $this->assign('node', $arr);
        $this->display();
    }

    public function addNode()
    {
        $this->assign('pid', I('pid'));
        $this->assign('level', I('level'));

        $this->display();
    }

    public function addnode_post()

    {


        $result = M('node')->add($_POST);
        if ($result) {
            $this->success('ok');

        } else {
            $this->error('fail');
        }
    }


    public function addRole()
    {
        $this->display();

    }

    public function addRole_post()
    {
        $name = I('name');
        $remark = I('remark');
        $statusk = I('status');
        $data = array('name' => I('name'),
            'remark' => I('remark'),
            'status' => I('status'));
        $result = M('role')->add($data);
        if ($result) {
            $this->Success('成功');
        } else {
            $this->error('失败');
        }
    }

    public function addUser()
    {
        $this->roles = M('role')->select();
        $this->display();
    }

    public function adduser_post()
    {

        $data = array('username' => I('username'),
            'userpwd' => I('userpwd'));
        $roleids = array_unique(I('role_id'));
        if ($uid = M('User')->add($data)) {
            $tmp = array();
            foreach ($roleids as $roleid) {
                $tmp[] = array('role_id' => $roleid,
                    'user_id' => $uid);
            }
            if (M('role_user')->addAll($tmp)) {
                $this->success('ok');
            } else {
                $this->error('fail');
            }

        }

    }

    public function access()
    {


        $field = array('id', 'name', 'title', 'pid', 'level');
        $node = M('node')->field($field)->select();
        $arr = sortnode($node);

        $rid = I('rid');
        $this->assign('rid', $rid);


        $access = M('access')->where(array('role_id' => $rid))->getField('node_id', true);

        $tep = $this->addaccess($arr, $access);

        $this->assign('node', $tep);


        $this->display();
    }

    public function access_post()
    {

        $arr = array();
        $rid = I('role_id');
        foreach (I('access') as $item) {
            $tmp = explode("_", $item);

            $arr[] = array('role_id' => $rid,
                'node_id' => $tmp[0],
                'level' => $tmp[1]);
        }
        M('access')->where(array('role_id' => $rid))->delete();
        $result = M('access')->addAll($arr);
        if ($result) {
            $this->success('ok');
        } else {
            $this->error('fail');
        }


    }

    /**
     * @param $list
     * @param $in_array 判断数在不在这个数组里
     */
    public function addaccess($nodes, $inarray)
    {
        $tt = array();
        foreach ($nodes as $item) {
            if (!empty($item['child'])) {
                $item['child'] = $this->addaccess($item['child'], $inarray);
            }
            if (in_array($item['id'], $inarray)) {
                $item['access'] = 1;
            } else {
                $item['access'] = 0;
            }
            $tt[] = $item;


        }
        return $tt;

    }


}

?>